package com.youth.youthservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youth.commonutils.R;
import com.youth.commonutils.RedisConstants;
import com.youth.youthservice.entity.announcement.UserAnnounceTags;
import com.youth.youthservice.mapper.UserAnnounceTagsMapper;
import com.youth.youthservice.service.UserAnnounceTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangqicai
 * @since 2023-03-23
 */
@Service
public class UserAnnounceTagsServiceImpl extends ServiceImpl<UserAnnounceTagsMapper, UserAnnounceTags> implements UserAnnounceTagsService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*向用户公告表里面插入数据，同时在用户收件箱里面插入数据*/
    @Override
    public boolean insertInfo(String announcementId, ArrayList<String> stuArrays, Integer userType) {
        /*stuArrays这个里面是用户的id，但是可能存在重复的id，发送消息的时候有重复，需要去除重复*/
        stuArrays.stream().distinct().collect(Collectors.toList())
                .forEach(userId -> {
                    /*向数据库里面添加数据*/
                    UserAnnounceTags userAnnounceTags = new UserAnnounceTags();
                    userAnnounceTags.setUserId(userId);
                    userAnnounceTags.setUserType(userType);
                    userAnnounceTags.setAnnounceId(announcementId);
                    int insert = baseMapper.insert(userAnnounceTags);
                    /*保存信息到redis里面*/
                    if(insert>0){
                        /*1.根据用户id构造key*/
                        String userKey= RedisConstants.ANNOUNCE_SAVE_KEY +userId;
                        /*2.将公告id放入到set集合里面*/
                        stringRedisTemplate.opsForSet().add(userKey, announcementId);
                    }
                });
        return true;
    }

    /*根据公告id删除用户公告表里面的记录数据，并删除redis里面的数据*/
    @Override
    public boolean deleteInfo(String annoId) {
        /*根据annoId获取对应的用户id集合*/
        List<UserAnnounceTags> announceTags = baseMapper.selectList(new QueryWrapper<UserAnnounceTags>().eq("announce_id", annoId).select("user_id"));
        /*删除记录*/
        int delete = baseMapper.delete(new QueryWrapper<UserAnnounceTags>().eq("announce_id", annoId));
        if (delete>0){
            announceTags.stream()
                    .forEach(userAnnounceTag -> {
                        String userId = userAnnounceTag.getUserId();
                        /*-删除redis里面的数据*/
                        String userKey=RedisConstants.ANNOUNCE_SAVE_KEY+userId;
                        stringRedisTemplate.opsForSet().remove(userKey,annoId);
                    });
        }
        return true;
    }

    /*根据公告id查询其所有用户id集合*/
    @Override
    public List<UserAnnounceTags> selectUsersByannoId(String annoId,String flag) {
        QueryWrapper<UserAnnounceTags> wrapper = new QueryWrapper<UserAnnounceTags>().eq("announce_id", annoId);
        if(!StringUtils.isEmpty(flag)){
            wrapper.eq("read_flag", flag);
        }
        List<UserAnnounceTags> userAnnounceTags = baseMapper.selectList(wrapper);
        return userAnnounceTags;
    }

    /*根据公告id查询其阅读量*/
    @Override
    public String selectUserViews(String annoId) {
        Integer views = baseMapper.selectCount(new QueryWrapper<UserAnnounceTags>().eq("announce_id", annoId).eq("read_flag", 1));
        return views.toString();
    }

    /*根据用户id得到所有的公告id的集合*/
    @Override
    public List<UserAnnounceTags> selectAnnoByUserId(String userId) {
        return baseMapper.selectList(new QueryWrapper<UserAnnounceTags>().eq("user_id", userId));
    }

    @Override
    public R confirm(String annoId) {
        UserAnnounceTags userAnnounceTags = new UserAnnounceTags();
        userAnnounceTags.setReadFlag(1);
        baseMapper.update(userAnnounceTags, new QueryWrapper<UserAnnounceTags>().eq("announce_id", annoId));
        return R.ok();

    }

}
